package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.winery.ActivityRank;
import com.yanqu.road.entity.winery.UserWineryInfoData;
import com.yanqu.road.utils.string.StringUtils;


import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserWineryInfoDaoImpl extends TempDataObjectDao<UserWineryInfoData> {
    public static String getUserWineryInfoDataSql = "select * from `t_u_winery_info` where `userId`=?";
    public UserWineryInfoData getUserWineryInfoData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserWineryInfoDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserWineryInfoDataSql = "update `t_u_winery_info` set aromaValue=?,artValue=?,badgeLevel=?,currentDrinkerId=?,currentDrinkerFeelingOfDrunk=?,currentDrinkerFriendlyValue=?,currentDrinkerIncome=?,currentDrinkerDrunkNum=?,initReward=?,rankActivityDataMap=?,makeWineryNotEnoughRequest=?,pausePatron=?,createAt=?,updateAt=? where userId=?";
    @Override
    protected boolean updateData(UserWineryInfoData userWineryInfoData) {
        userWineryInfoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userWineryInfoData.getAromaValue()));
        para.put(i++, new DbParameter(Types.BIGINT, userWineryInfoData.getArtValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getBadgeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerFeelingOfDrunk()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerFriendlyValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerIncome()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerDrunkNum()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getInitReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWineryInfoData.getRankActivityDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getMakeWineryNotEnoughRequest()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWineryInfoData.getPausePatron())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryInfoData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryInfoData.getUpdateAt().getTime())));
        para.put(i++, new DbParameter(Types.BIGINT, userWineryInfoData.getUserId()));
        if (getDb().executeNoneQuery(updateUserWineryInfoDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserWineryInfoDataSql = "insert into `t_u_winery_info`(userId,aromaValue,artValue,badgeLevel,currentDrinkerId,currentDrinkerFeelingOfDrunk,currentDrinkerFriendlyValue,currentDrinkerIncome,currentDrinkerDrunkNum,initReward,rankActivityDataMap,makeWineryNotEnoughRequest,pausePatron,createAt,updateAt) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserWineryInfoData userWineryInfoData) {
        userWineryInfoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userWineryInfoData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userWineryInfoData.getAromaValue()));
        para.put(i++, new DbParameter(Types.BIGINT, userWineryInfoData.getArtValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getBadgeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerFeelingOfDrunk()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerFriendlyValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerIncome()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getCurrentDrinkerDrunkNum()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getInitReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWineryInfoData.getRankActivityDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryInfoData.getMakeWineryNotEnoughRequest()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWineryInfoData.getPausePatron())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryInfoData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryInfoData.getUpdateAt().getTime())));
        if (getDb().executeNoneQuery(insertUserWineryInfoDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserWineryInfoData parseData(ResultSet rs) throws SQLException {
        UserWineryInfoData userWineryInfoData = new UserWineryInfoData();
        userWineryInfoData.setUserId(rs.getLong("userId"));
        userWineryInfoData.setAromaValue(rs.getLong("aromaValue"));
        userWineryInfoData.setArtValue(rs.getLong("artValue"));
        userWineryInfoData.setBadgeLevel(rs.getInt("badgeLevel"));
        userWineryInfoData.setCurrentDrinkerId(rs.getInt("currentDrinkerId"));
        userWineryInfoData.setCurrentDrinkerFeelingOfDrunk(rs.getInt("currentDrinkerFeelingOfDrunk"));
        userWineryInfoData.setCurrentDrinkerFriendlyValue(rs.getInt("currentDrinkerFriendlyValue"));
        userWineryInfoData.setCurrentDrinkerIncome(rs.getInt("currentDrinkerIncome"));
        userWineryInfoData.setCurrentDrinkerDrunkNum(rs.getInt("currentDrinkerDrunkNum"));
        userWineryInfoData.setInitReward(rs.getInt("initReward"));
        userWineryInfoData.setRankActivityDataMap(JSON.parseObject(rs.getString("rankActivityDataMap"), new TypeReference<Map<Integer, ActivityRank>>() {}));
        userWineryInfoData.setMakeWineryNotEnoughRequest(rs.getInt("makeWineryNotEnoughRequest"));

        String pausePatron = rs.getString("pausePatron");
        if(!StringUtils.isNullOrEmpty(pausePatron)) {
            userWineryInfoData.setPausePatron(JSON.parseObject(pausePatron,new TypeReference<List<Integer>>() {}));
        }else {
            userWineryInfoData.setPausePatron(new ArrayList<Integer>());
        }


        userWineryInfoData.setCreateAt(rs.getTimestamp("createAt"));
        userWineryInfoData.setUpdateAt(rs.getTimestamp("updateAt"));

        return userWineryInfoData;
    }




}
